package com.markus.code.newcode;

import com.markus.code.图.图2.Node;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/3 17:24
 * todo:
 */
public class LRU {
    public static class LRUCache<K,V> extends LinkedHashMap{
        private final int CACHE_SIZE;
        public LRUCache(int cacheSize){
            super((int) (Math.ceil(cacheSize/0.75)+1),0.75f,true);
            CACHE_SIZE = cacheSize;
        }
        @Override
        protected boolean removeEldestEntry(Map.Entry eldest){
            return size()>CACHE_SIZE;
        }
    }
    /**
     * lru design
     * @param operators int整型二维数组 the ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    public int[] LRU (int[][] operators, int k) {
        // write code here
        LRUCache<Integer,Integer> lru = new LRUCache<>(k);
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<operators.length;i++){
            //==1表示set
            if (operators[i][0] == 1){
                int key = operators[i][1];
                int value = operators[i][2];
                lru.put(key,value);
            }else{
                int key = operators[i][1];
                Integer integer = (Integer) lru.get(key);
                if (integer == null){
                    integer = -1;
                }
                list.add(integer);
            }
        }
        return getArr(list);
    }
    private int[] getArr(List<Integer> list){
        int []arr= new int[list.size()];
        int index = 0;
        for (Integer integer : list) {
            arr[index++] = integer;
        }
        return arr;
    }

    public static void main(String[] args) {
        int arr[][] = {{1,1,1},{1,2,2},{1,3,2},{2,1},{1,4,4},{2,2}};
        int size = 3;
        LRU demo = new LRU();
        int[] lru = demo.LRU(arr, size);
        for (int i : lru) {
            System.out.print(i+" ");
        }
    }
}
